package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.wallet.SundayWalletRecord;
import org.itboys.mobile.service.mysql.web.cash.SundayCashService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhangyang on 2018/1/31.
 * 圣代_提现_后台_controller
 */
@Controller
@RequestMapping("/sunday/web/cash")
public class SundayCashController extends BaseController{


    @Autowired
    private SundayCashService cashService;

    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request, Model model){
        return "/sunday/cash/index";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,HttpServletResponse response){
        try {
          //  PageResult<SundayWalletRecordDto> result = cashService.select(request);
            PageResult<SundayWalletRecord> result = cashService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/cash/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 处理提现申请:同意/驳回
     * @param request
     * @param response
     * @param id    记录id
     * @param type  类型:2-同意,3-驳回
     */
    @RequestMapping("/operate")
    public void operate(HttpServletRequest request,HttpServletResponse response,
                     @RequestParam(value="id",required=true) Long id,
                     @RequestParam(value="type",required=true) Integer type){
        try {
            cashService.operate(id,type);
            ResponseMessage.success();
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/cash/operate------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }



}
